home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #6 / Amiga Plus CD - 2004 - No. 06.iso / AmiSoft / Comm / misc / trsi-ftpd01.lha / FAME-FTPd / source / MakeArray.c < prev    next >
C/C++ Source or Header  |  2004-04-24  |  2KB  |  115 lines

  1. /*
  2.  *  MakeArray.c - Creates a array of Char * out of a given Textline
  3.  *
  4.  *  Original by Spy/tRSi - Rewritten and bugfixed by SieGeL/tRSi
  5.  */
  6. #include <stdlib.h>
  7. #include <strings.h>
  8. #include <exec/memory.h>
  9. #include <proto/exec.h>
  10. #include <proto/fame.h>
  11. #include <libraries/fame.h>
  12. #include <fame/fame.h>
  13. #include "struct_ex.h"
  14. #include "proto.h"
  15.  
  16. struct StringList
  17.     {
  18.   char *String ;
  19.   struct StringList *next ;
  20.     }*sl1,*sl2 ;
  21.  
  22. static void Free_Structs(void);
  23.  
  24. static BOOL INSERT (char *s)
  25.     {
  26.   if (!sl1)
  27.       {
  28.         if(!(sl1 = (struct StringList *) AllocPooled (mem_pool,sizeof (struct StringList)))) return(FALSE);
  29.     sl2 = sl1;
  30.       }
  31.   else
  32.       {
  33.         if(!(sl2->next = (struct StringList *) AllocPooled (mem_pool,sizeof (struct StringList)))) return(FALSE);
  34.     sl2 = sl2->next;
  35.       }
  36.   sl2->next = NULL;
  37.   if(!(sl2->String = (char *) AllocPooled (mem_pool,(strlen(s)+2)))) return(FALSE);
  38.   strcpy (sl2->String,s);
  39.     return(TRUE);
  40.     }
  41.  
  42. static void MoveStrings (char **res,struct StringList *sl)
  43.     {
  44.     struct     StringList *slb;
  45.     char         **strs;
  46.  
  47.   slb = sl;
  48.   strs = res;
  49.   while(slb)
  50.       {
  51.     *strs = slb->String;
  52.     strs++;
  53.     slb = slb->next;
  54.       }
  55.     }
  56.  
  57. char **MakeArray (char *s,char sep)
  58.     {
  59.     char     *str1,*str2,**result;
  60.     int     i;
  61.  
  62.   sl1 = NULL;
  63.     sl2 = NULL;
  64.   str1 = s;
  65.   i = 0;
  66.     while((str2 = FAMEStrChr(str1,sep)))
  67.       {
  68.     *str2 = 0;
  69.     if(INSERT (str1)==FALSE) return(NULL);
  70.     i++;
  71.     *str2 = ' ';
  72.     while ((*str2 != 0) && (*str2 == sep))
  73.     str2++;
  74.     str1 = str2;
  75.       }
  76.   if (*str1 != 0)
  77.       {
  78.         if(INSERT (str1)==FALSE) return(NULL);
  79.         }
  80.   i++;
  81.   if(!(result = (char **) AllocPooled (mem_pool,(i+1)*4))) return(NULL);
  82.   MoveStrings (result,sl1);
  83.   return (result);
  84.     }
  85.  
  86. void FreeArray (char **strings)
  87.     {
  88.     char     **strs;
  89.     int     i;
  90.  
  91.     i = 0 ;
  92.   strs = strings;
  93.   while (*strs)
  94.       {
  95.     i++ ;
  96.     FreePooled (mem_pool,*strs,sizeof(*strs));
  97.     strs++ ;
  98.       }
  99.   i++ ;
  100.     FreePooled(mem_pool,strings,sizeof(strings)*(i+1));
  101.     Free_Structs();
  102.     }
  103.  
  104. static void Free_Structs(void)
  105.     {
  106.     struct StringList *h;
  107.     while(sl1)
  108.         {
  109.         h=sl1;
  110.         sl1=sl1->next;
  111.         FreePooled(mem_pool,h,sizeof(struct StringList));
  112.         }
  113.     sl1=NULL;sl2=NULL;
  114.     }
  115.